package com.cmys.money.cloudshop.controller.index;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cmys.money.cloudshop.model.product.ProductType;
import com.cmys.money.cloudshop.service.product.ProductService;
import com.cmys.money.cloudshop.service.shopping.ShoppingService;
import com.cmys.money.cloudshop.service.template.TemplateService;

@Controller
@RequestMapping("/app/shopping") 
public class IndexController {
	
	protected final Log logger = LogFactory.getLog(getClass());
	private final TemplateService templateService;
	private final ProductService productService;
	
	@Resource(name="shoppingService")
	private ShoppingService shoppingService;
	
	@Autowired
    public IndexController(TemplateService templateService,
			ProductService productService) {
		super();
		this.templateService = templateService;
		this.productService = productService;
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index() {
    	logger.info("info");
    	logger.debug("debug");
		return "index";
	}
    
    @RequestMapping(value = "/createHtml", method = RequestMethod.GET)
    public void createHtml(){
    	Map<String, Object> map = new HashMap<String, Object>();
        String username = "中国";
        map.put("username", username);
        
        //首页精彩活动推荐
        map.put("jchdList", shoppingService.getJchdList());
        
        //首页特价热卖
        map.put("tjrmList", shoppingService.getTdrmList());
        
        //商城公告
        map.put("scggList", shoppingService.getScggList());
        
        //促销信息
        map.put("cxxxList", shoppingService.getCxxxList());
        
        //所有商品分类
        Collection<ProductType> list = shoppingService.getShopSortList();
        map.put("shopSortList", list);
        //ProductType root = productService.findProductTypeTree(new Long(1));
        //map.put("shopSortList", root);
        
        String htmlPath = System.getProperty("user.dir") + "/cloudshop-web/src/main/webapp/resources/demo/";
		//templateService.createHtml("/testfile/" , "user.ftl", map, htmlPath);
    	//templateService.createHtml(templatePath, templateName, map, htmlPath);
//        templateService.createHtml("/" , "header_1.ftl", map, htmlPath);
//        templateService.createHtml("/" , "header_2.ftl", map, htmlPath);
//        templateService.createHtml("/" , "index.ftl", map, htmlPath);
//        templateService.createHtml("/" , "shopSelect.ftl", map, htmlPath);
    	System.out.println("is ok");
    }
    
//    /**
//     * 首页商品分类
//     * @return List
//     */
//    public Collection<ProductType> getShopSortList (){
//    	Collection<ProductType> list = productTypeDao.findAll();
//        return list;
//    }
    
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public void test() {
		Map<String, Object> map = new HashMap<String, Object>();
		ProductType productTypeRoot = productService.findProductTypeTree(new Long(1));
		
		ProductType productTypeRoot1 = productService.findProductTypeNavigation(new Long(4));
		map.put("productTypeRoot", productTypeRoot);
		map.put("productTypeRoot1", productTypeRoot1);
		String htmlPath = System.getProperty("user.dir")
				+ "/cloudshop-web/src/main/webapp/resources/demo/";
		templateService.createPage("/", "test.ftl", htmlPath, "test.html", map);
		
		
//		map.put("username", "uuuu");
//		templateService.createHtml("/", "user.ftl", map, htmlPath);
	}
    
   
}
